const arr = [2, 8, 5, 1, 3, 2, 0]

// function quick_sort(arr) {
//   if (arr.length <= 1) return arr
//   let l = []
//   let r = []
//   const middle = arr.splice(arr.length >> 1, 1)[0]
//   for (let key in arr) {
//     if (arr[key] > middle) r.push(arr[key])
//     else l.push(arr[key])  
//   }
//   return [...quick_sort(l), middle, ...quick_sort(r)]
// }

function quick_sort(arr) {
  if (arr.length <= 1) return arr
  const l = [], r = []
  const mid = arr.splice(arr.length >> 1, 1)[0]
  for (const val of arr) {
    if (val < mid) l.push(val)
    else r.push(val)
  }
  return [...quick_sort(l), mid, ...quick_sort(r)]
}

console.log(quick_sort(arr));
